package com.cdh.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdh.edu.entity.Course;
import com.cdh.edu.entity.vo.CourseInfoVo;
import com.cdh.edu.entity.vo.CoursePublishVo;
import com.cdh.edu.entity.vo.CourseQueryVo;
import com.cdh.edu.entity.vo.CourseVo;
import com.cdh.edu.service.CourseService;
import com.cdh.edu.service.VideoService;
import com.cdh.utils.result.Restful;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cdh
 * @since 2020-10-20
 */
@ApiModel(description = "课程管理")
@RestController
@RequestMapping("course")
@CrossOrigin
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private VideoService videoService;

    //课程列表
    @GetMapping("getCourseList")
    public Restful getCourseList(){
        List<Course> list = courseService.list(null);
        return Restful.ok().data("list",list);
    }

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public Restful addCourseInfo(@RequestBody CourseInfoVo courseInfo){
        //返回添加之后的课程ID，为了后面添加大纲使用
        String courseId = courseService.saveCourseInfo(courseInfo);
        return Restful.ok().data("courseId",courseId);
    }

    // 根据课程ID查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public Restful getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return Restful.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public Restful updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Restful.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public Restful getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getPublishCourseInfoByCourseId(courseId);
        return Restful.ok().data("publishInfo",coursePublishVo);
    }

    // 课程最终发布 修改课程状态
    @PostMapping("publishCourse/{courseId}")
    public Restful publishCourse(@PathVariable String courseId){
        Course course = new Course();
        course.setId(courseId);
        course.setStatus(course.COURSE_NORMAL);
        courseService.updateById(course);
        return Restful.ok();
    }


    // TODO 完善条件查询带分页
    // 分页查询课程  current 当前页 limit 每页记录数
    @ApiOperation(value = "课程分页列表")
    @PostMapping("pageCourse/{current}/{limit}")
    public Restful pageTeacherCondition(@PathVariable long current
            , @PathVariable long limit
            ,@RequestBody(required = false) CourseQueryVo cq){
        IPage<CourseVo> pageCourse = courseService.selectPage(current,limit,cq);
        long total = pageCourse.getTotal();//总记录数
        List<CourseVo> records = pageCourse.getRecords();
        return Restful.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "逻辑删除课程")
    @DeleteMapping("remove/{id}")
    public Restful removeTeacher(@ApiParam(name = "id",value = "课程ID",required = true)
                                     @PathVariable String id){
        //TODO:删除课程视频 调用VOD中的删除视频文件的接口
        videoService.removeVideoByChapterId(id);
        //删除课程封面
        courseService.removeCoverById(id);

        boolean flag = courseService.deleteCourseById(id);
        if (flag){
            return Restful.ok().message("课程删除成功");
        }else {
            return Restful.error();
        }
    }
}

